package com.fanghaoran.view.manager;

import com.fanghaoran.controller.ManagerController;
import com.fanghaoran.entity.Book;
import com.fanghaoran.entity.Words;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author: win11
 * @Date: 2023/03/28/1:21
 * @Description:
 */
public class BookPanel extends JPanel {
    /**
     * 单词书名,默认四级书
     */
    private String bookName = "CET-4";
    private JTable table;

    public BookPanel() throws Exception {
        this.setLayout(new BorderLayout());
        //功能栏
        JPanel btnPanel = new JPanel();
        btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        //新增单词书功能按钮
        JButton addBook = new JButton("新增单词书");

        //搜索
        Box box3 = Box.createHorizontalBox();
        //获取书籍数据
        @SuppressWarnings("unchecked")
        List<Book> books = (List<Book>) new ManagerController().getBookInfo().getMessage();
        //存到selectList选项集合
        List<String> selectList = new ArrayList<>();
        for (Book book : books) {
            selectList.add(book.getName());
        }
        //转成vector对象
        Vector<String> vector = new Vector<>(selectList);
        //选项列表
        JComboBox<String> selector = new JComboBox<>(vector);
        //搜索按钮
        JButton searchWordsOfBook = new JButton("搜索该书单词");
        box3.add(Box.createHorizontalStrut(80));
        box3.add(selector);
        box3.add(searchWordsOfBook);
        box3.add(Box.createHorizontalStrut(100));

        //单词增删改功能按钮
        JButton addWord = new JButton("新增单词");
        JButton deleteWord = new JButton("删除单词");
        JButton modifyWord = new JButton("修改单词");

        btnPanel.add(addBook);
        btnPanel.add(box3);
        btnPanel.add(addWord);
        btnPanel.add(deleteWord);
        btnPanel.add(modifyWord);

        this.add(btnPanel, BorderLayout.NORTH);

        //选项条目列表点击事件
        selector.addItemListener(e -> {
                    if (selector.getSelectedItem() != null) {
                        for (String s : selectList) {
                            if (selector.getSelectedItem().equals(s)) {
                                //确认当前要添加单词的单词书
                                bookName = s;
                            }
                        }
                    }
                }
        );
        //给按钮设置点击事件
        addBook.addActionListener(e -> {
                    try {
                        new AddBookFrame();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
        );
        searchWordsOfBook.addActionListener(e -> {
                    //返回选定单词书的单词数据
                    List<Words> wordsList = null;
                    try {
                        wordsList = (List<Words>) new ManagerController().selectWordOfBook(bookName).getMessage();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    //更新表格数据
                    table.setModel(new WordInfoTableModel(wordsList));
                }
        );
        deleteWord.addActionListener(e -> {
                    try {
                        new DeleteWordFrame();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
        );
        modifyWord.addActionListener(e -> {
                    //获取当前表格选中的条目
                    int selectedRow = table.getSelectedRow();
                    //如果有选择条目,返回行数,没有则返回-1
                    if (selectedRow == -1) {
                        JOptionPane.showMessageDialog(this, "请选择要修改的单词条目");
                        return;
                    }
                    // 获取选中的行数据
                    Object[] rowData = new Object[table.getColumnCount()];
                    for (int i = 0; i < rowData.length; i++) {
                        rowData[i] = table.getValueAt(selectedRow, i);
                    }
                    //开启修改页面
                    try {
                        new ModifyWordFrame(rowData);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
        );
        addWord.addActionListener(e -> {
                    try {
                        new AddWordFrame();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
        );

        //展示信息的表格
        //数据库获取所有单词书信息
        @SuppressWarnings("unchecked")
        List<Book> booksList = (List<Book>) new ManagerController().getBookInfo().getMessage();
        table = new JTable(new BookInfoTableModel(booksList));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //放在panel中间位置
        this.add(new JScrollPane(table), BorderLayout.CENTER);

    }
}
